package edu.chinasoft.servlet;

import edu.chinasoft.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author cjogng
 * @date 2020年1月19日:下午4:21:41
 * @version V1.0
 * @Description:
 *
 */
@WebServlet(name="myLogoutUserForServlet",value="/servlet/logoutuserforservlet")
public class  LogoutUserForServlet  extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(ServletForLogin.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.debug(" LogoutUserForServlet >doGet start.......");
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        User user=null;
        if(session!=null) {
            user= (User)session.getAttribute("user");
        }
        if(user==null) {      //表示用户没有登录
            return;
        }else {                //表示用户登录
            session.removeAttribute("user");
            response.sendRedirect(""+request.getContextPath()+"/main.jsp");
        }
        logger.debug(" LogoutUserForServlet >doGet end.......");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.debug(" LogoutUserForServlet >doPost start.......");
        this.doGet(request, response);

        logger.debug(" LogoutUserForServlet >doPost end.......");
    }

}